package com.mengdie.shuidi.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mengdie.shuidi.AppContext;
import com.orhanobut.logger.Logger;

import java.util.List;


/**
 * Created by apple on 17/2/3.
 */

public class URLManager {

    private static final String SP_BASE_URL = "sp_base_url";
    private static final String SP_BASE_URL_LIST = "sp_base_url_list";
    private static final String SP_BACK_URL = "sp_back_url";

    private static String BASE_URL;


    private static Context mAppContext = AppContext.getInstance();
    public static String PAY_URL;


    public static String getBaseUrl() {
        if (BASE_URL == null) {
            synchronized (URLManager.class) {
                SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
                BASE_URL = sp.getString(SP_BASE_URL, "");
                if (TextUtils.isEmpty(BASE_URL)) {
                    BASE_URL = "http://api.beijingsongshui.com/";
                }
            }
        }
        return BASE_URL;
    }


    public static void setBaseUrl(String baseUrl) {
        BASE_URL = baseUrl;
        SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
        sp.edit().putString(SP_BASE_URL, baseUrl).commit();
    }


    public static void setBackUrl(String backUrl) {
        SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
        sp.edit().putString(SP_BACK_URL, backUrl).commit();
    }

    /**
     * 更新 url
     *
     * @param size
     */
    public static void updateBaseUrl(int size) {
        SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
        String jsonList = sp.getString(SP_BASE_URL_LIST, "");
        Gson gson = new Gson();
        List<String> datalist = gson.fromJson(jsonList, new TypeToken<List<String>>() {
        }.getType());
        int n = (int) (Math.random() * (datalist.size() - 1) + 1);
        PAY_URL = "http://" + datalist.get(size) + "/";
        BASE_URL = "http://" + datalist.get(size) + "/api/";
        // setBaseUrlList(datalist);
        //int n = (int) (Math.random() * datalist.size());

    }

    /**
     * 设置 urlList
     *
     * @param urlList
     */
    public static void setBaseUrlList(List<String> urlList) {
        SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = gson.toJson(urlList);
        sp.edit().putString(SP_BASE_URL_LIST, json).apply();
    }

    public static String getBaseUrlRandom() {
        if (BASE_URL == null) {
            synchronized (URLManager.class) {
                SharedPreferences sp = mAppContext.getSharedPreferences(SP_BASE_URL, Context.MODE_PRIVATE);
                String jsonList = sp.getString(SP_BASE_URL_LIST, "");
                Gson gson = new Gson();
                List<String> datalist = gson.fromJson(jsonList, new TypeToken<List<String>>() {
                }.getType());

                int n = (int) (Math.random() * (datalist.size() - 1) + 1);
                Logger.e("getBaseUrlRandom  n= " + n);
                PAY_URL = "http://" + datalist.get(0) + "/";
                BASE_URL = "http://" + datalist.get(0) + "/api/";
            }
        }
        return BASE_URL;
//        return "http://t_api.zhimadaili.com/api/";
    }


}
